//
//  LanguageTBV.m
//  Community-for-college
//
//  Created by 梁海洋 on 2016/12/6.
//  Copyright © 2016年 彭鑫. All rights reserved.
//

#import "LanguageTBV.h"
#import "LanguageCell.h"
#import "TranslateLanguageModel.h"
@interface LanguageTBV ()
@property (nonatomic,strong)NSArray *languageArray;
@property (nonatomic,strong)NSArray *flagArray;
@end

@implementation LanguageTBV

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册cell
    [self.tableView registerClass:[LanguageCell class] forCellReuseIdentifier:@"LanguageCell"];
   
    //初始化array
    _languageArray = [NSArray array];
    _languageArray = @[@"中文 Chinesa|zh",@"英文 English|en",@"粤语 YueYu|yue",@"文言文 GuWen|wyw",@"日语 Japanese|jp",@"韩语 Korean|kor",@"法语 French|fra",@"西班牙语 Spanish|spa",@"泰语 Thai|th",@"俄语 Russian|ru",@"德语 German|de",@"意大利语 Italian|it",@"希腊语 Greek|el",@"荷兰语 Dutch|nl",@"波兰语 Polish|pl",@"保加利亚语 Bulgarian|bul",@"丹麦语 Danish|dan",@"芬兰语 Finnish|fin",@"捷克语 Czech|cs",@"瑞典 Sweden|swe"];
    //旗子
    _flagArray = [NSArray array];
    _flagArray = @[@"🇨🇳",@"🇬🇧",@"🇭🇰",@"🇨🇳",@"🇯🇵",@"🇰🇷",@"🇫🇷",@"🇪🇸",@"🇹🇭",@"🇷🇺",@"🇩🇪",@"🇮🇹",@"🇬🇷",@"🇳🇱",@"🇵🇱",@"🇧🇬",@"🇩🇰",@"🇫🇮",@"🇨🇿",@"🇸🇪"];
    NSLog(@"------>>>%ld\n%ld",_languageArray.count,_flagArray.count);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _languageArray.count;
}


- (LanguageCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"LanguageCell";
    
    LanguageCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    //支持的语言
    NSString *srcStr = _languageArray[indexPath.row];
    NSArray *languageStr = [srcStr componentsSeparatedByString:@"|"];
    cell.languageLabel.text = languageStr[0];
    //国旗显示
    cell.flagLabel.text = _flagArray[indexPath.row];
    
    //显示选择的语言
    if ([_redLanguage isEqualToString:languageStr[1]]||[_blueLanguage isEqualToString:languageStr[1]]) {
        cell.selectLabel.hidden = NO;
    }
    else
    {
        cell.selectLabel.hidden = YES;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"LanguageCell";
    
    LanguageCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.selectLabel.hidden = NO;
    
        NSString *srcStr = _languageArray[indexPath.row];
        NSArray *languageStr = [srcStr componentsSeparatedByString:@"|"];
     if([_fromWhere isEqualToString:@"redBtn"]){
         
        TranslateLanguageModel *translateLanguageModel = languageStr[1];
        //选择翻译语言
        [UserDefaults setObject:translateLanguageModel.languageCode forKey:@"redTranslateLanCode"];
        [UserDefaults setObject:translateLanguageModel.languageShowName forKey:@"redTranslateLanName"];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }else if([_fromWhere isEqualToString:@"blueBtn"]){
        TranslateLanguageModel *translateLanguageModel = languageStr[1];
        //选择翻译语言
        [UserDefaults setObject:translateLanguageModel.languageCode forKey:@"blueTranslateLanCode"];
        [UserDefaults setObject:translateLanguageModel.languageShowName forKey:@"blueTranslateLanName"];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
